package com.cloudfun.campusshare.device.protocol.consumer;

import com.cloudfun.campusshare.common.constant.RabbitConstants;
import com.cloudfun.campusshare.device.protocol.constant.PumpConstant;
import com.cloudfun.campusshare.device.protocol.response.*;
import com.cloudfun.campusshare.service.DevicePropertyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloudfun.msgsubscribe.DeviceInfo;
import org.springframework.util.StopWatch;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangxiyuan
 * @description 液体泵响应消息进行 消费
 * @createDate 2021/5/19 9:46
 **/
@Slf4j
@Service
public class PumpDeviceConsumer implements DeviceMessageConsumer{
    @Autowired
    private DevicePropertyService devicePropertyService;

    @Override
    public boolean support(Class<?> clazz) {
        return PumpInfo.class.isAssignableFrom(clazz);
    }

    @Override
    public void doConsumer(BaseMessage message, String imei) {
        // 根据imei 查询设备相关属性
        Map<String, String> propertiesInDB = devicePropertyService.getDevicePropertiesByImei(imei);
        Map<String, String> propertiesFromDevice = new HashMap<>();
        if(message.getClass().isAssignableFrom(AllPumpStatus.class)){
            AllPumpStatus allPumpStatus = (AllPumpStatus) message;
            List<PumpStatus> list = allPumpStatus.getList();
            // 转换成 key为 pumpStatus + 泵的号码，value为状态值
            propertiesFromDevice = list.stream().collect(Collectors.toMap(s -> PumpConstant.PUMP_STATUS + s.getPumpNum(), s -> s.getStatus() + ""));
        }else if(message.getClass().isAssignableFrom(AllPumpWorkInfo.class)){
            // 这一块逻辑是调用命令07时触发，但后面经过讨论之后 工作时间以及延迟时间不需要获取处理
            AllPumpWorkInfo allPumpWorkInfo = (AllPumpWorkInfo) message;
            List<PumpWorkInfo> list = allPumpWorkInfo.getList();
            for (PumpWorkInfo pumpWorkInfo :
                    list) {
                propertiesFromDevice.put(PumpConstant.PUMP_WORK_TIME + pumpWorkInfo.getPumpNum(), pumpWorkInfo.getWorkTime() + "");
                propertiesFromDevice.put(PumpConstant.PUMP_DELAY_TIME + pumpWorkInfo.getPumpNum(), pumpWorkInfo.getDelayTime() + "");
            }
        }
        DeviceInfo deviceInfo = buildDevice(imei, message.getTimestamp());
        this.internalProcess(deviceInfo, propertiesInDB, propertiesFromDevice);
    }

    private DeviceInfo buildDevice(String imei, String timestamp){
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setImei(imei);
        deviceInfo.setTimestamp(timestamp);
        return deviceInfo;
    }

    private void internalProcess(DeviceInfo deviceInfo,Map<String, String> propertiesInDB, Map<String, String> propertiesFromDevice) {
        for (Map.Entry<String, String> entry : propertiesFromDevice.entrySet()) {
            String fieldName =  entry.getKey();
            String fieldValue = entry.getValue();
            if (StringUtils.isBlank(fieldValue)) {
                continue;
            }
            // 如果库中的值和解析出来的值一样 则跳过
            if (propertiesInDB.containsKey(fieldName) && propertiesInDB.get(fieldName).equals(fieldValue)) {
                continue;
            }
            deviceInfo.setAttr(fieldName, fieldValue);
            propertiesInDB.put(fieldName, fieldValue);
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("updatePumpProperty: " + deviceInfo.getImei());
        // 内部消费
        devicePropertyService.updatePumpProperty(propertiesInDB,deviceInfo);
        stopWatch.stop();
        log.info(stopWatch.prettyPrint());
    }

}
